<?php
/**
* This is a tool class to add unit tests scenario to webappkit kits
* @use Simpletest kit
* @author J.Ducastel <nospam0@ducastel.name>
*
*/
class SimpleTestTool extends WakTool {

    /**
    * @static
    */
    public static function toolFactory($kit,$cfg=null) {
        return new SimpleTestTool($kit,$cfg);
    }

    /**
    * runs kit tests
    * @access public
    * @return WakTestReporter
    */
    function run() {
        // loading simple test framework
        // $ST=Webappkit::getKit('simpletest');
        // building test reporter
        $this->reporter=new WakTestReporter();
        // preparing test environment
        if (!$this->setUp()) {
            // abort tests if setup fails
            $this->reporter->paintError("setUp() failed (returned false)");
            return $this->reporter;
            return false;
        }

        // running test classes
        $this->runSelfClasses($this->cfg['classes']);

        //else // running all existing test classes
        //    $this->runAllClasses();

        // cleaning environment after tests
        $this->tearDown();
        // print_r($this->reporter);
        return $this->reporter;
    }

    /**
    * this function is ran before tests
    * @return bool if false; tests are aborted
    */
    function setUp() {
        return true;
    }

    /**
    * this function is ran after tests
    * @return bool
    */
    function tearDown() {
        return true;
    }

    /**
    * @var array config data
    */
    public $cfg=array(
        'label'=>'Anonymous test scenario tool'
        ,'classes'=>array()
        );

    /**
     * run self test classes
     * those are defined in $cfg->classes as class => file
     * @return bool
     */
    function runSelfClasses() {
        if (!$this->cfg['classes'])
            return false;
        $group=new GroupTest("tests for {$this->kit}");
        $path=Webappkit::getPathFromId($this->kit);
        foreach ($this->cfg['classes'] as $class => $file) {
            // loading class def
            // echo $path.$file;
            try {
                Webappkit::loadLibFile($path.$file);
            } catch (WakMissingFile $e) {
                // file not found
            }
            if (!class_exists($class))
                throw new WakMissingClass("Unit tests case class $class is missing",404);
            // adding class to group test
            $group->addTestClass($class);
        }
        // running group test
        // $this->reporter=new SimpleReporter();
        return $group->run($this->reporter);
        // print_r($reporter);
    }
}
